#include "server.h"
#include "player.h"

Battlefield PlayerA_BF;
char * PlayerAName = "zhangxingnan";

static int board_weight[10][10]={{10,9,9,10,9,9,10,9,10},
                         {9,10,9,9,10,9,9,10,9,9},
                         {10,9,9,10,9,9,10,9,9,10},
                         {9,10,9,9,10,9,9,10,9,9},
                         {10,9,9,10,9,10,9,9,10,9},
                         {9,10,9,9,10,9,9,10,9},
                         {10,9,9,10,9,10,9,9,10,10},
                         {9,9,10,9,9,10,9,9,10,9},
                         {10,9,9,10,9,9,10,9,9,10},
                         {9,10,9,9,10,9,10,9,9,10}};
static int state_searchShip=1;
static int state_findShip=0; 
static int state_searchDirection=0; //搜索方向状态
static int state_firstsearchDirection=0;
static int state_findDirection=0; //找到了方向
static int state_findShipendEnd=0;
static int state_findShipFirstEnd=0;

//static int FindShipList[5]={0};
//static int FindShipListLenght=0;
static int board_state[10][10]={0}; //记录扫描的结果
static int notfirst=0; //记录开局状态
static int shipDirection[2]={0,0};
static int findshipFirstxy[2]={0,0};
static int Direction[4][2]={{0,-1},{-1,0},{1,0},{0,1}};
static int DirectionIndex=0;
static int x=0,y=0;
static int dx=0,dy=0;
// 玩家部署舰艇
int DeploymentA()
{

    for(int i=0;i<M;i++)
        for(int j=0;j<M;j++)
        {
            PlayerA_BF.area[i][j] = 1;
        }
    // add your arrangement, for example:
    PlayerA_BF.area[1][1] = D; PlayerA_BF.area[1][2] = D;
    PlayerA_BF.area[2][4] = S; PlayerA_BF.area[3][4] = S; PlayerA_BF.area[4][4] = S;
    PlayerA_BF.area[5][1] = C; PlayerA_BF.area[5][2] = C; PlayerA_BF.area[5][3] = C;
    PlayerA_BF.area[7][1] = B; PlayerA_BF.area[7][2] = B; PlayerA_BF.area[7][3] = B; PlayerA_BF.area[7][4] = B;
    PlayerA_BF.area[4][6] = A; PlayerA_BF.area[5][6] = A; PlayerA_BF.area[6][6] = A; PlayerA_BF.area[7][6] = A; PlayerA_BF.area[8][6] = A;

    printBF(&PlayerA_BF);
    return 0;
}

static int outrange(int x,int y){ //越界判断
    if(x>9 || x<0 || y>9 || y<0)
        return 0;
}
static int xySearchAble(int x,int y){ //判断当前xy点是否可以探测，排除超过边界点，和已经探索过的点。
    if(outrange(x,y))
        return 0;
    if(board_state[x][y]!=0)
        return 0;
    return 1;
}
//判断当前
static int findShip(int isHit){ 
    if(state_searchDirection){   //如果当前状态是搜索方向
        if(state_firstsearchDirection){
            DirectionIndex=0;
             while(!xySearchAble(x+Direction[DirectionIndex][0], y+Direction[DirectionIndex][1])){
                DirectionIndex+=1;
                if(DirectionIndex>=4){ 
                    DirectionIndex=0;
                    break;
                }
            }
            x=findshipFirstxy[0] + Direction[DirectionIndex][0];
            y=findshipFirstxy[1] + Direction[DirectionIndex][1];
            state_firstsearchDirection=0;
            
        }else{
            if(isHit){ //如果击中了，说明找到了方向
                state_findDirection=1; // 找到方向，设置为1
                state_searchDirection=0; //找到方向，取消寻找状态
                shipDirection[0]=Direction[DirectionIndex][0];
                shipDirection[1]=Direction[DirectionIndex][1];
                if(!xySearchAble(x+Direction[DirectionIndex][0], y+Direction[DirectionIndex][1])){ //如果下一步搜索超出边界直接改变状态
                    state_findShipFirstEnd=1;
                    x=findshipFirstxy[0];
                    y=findshipFirstxy[1];
                }else{ //一切正常，设置下一步搜索位置
                    x=x+shipDirection[0];
                    y=y+shipDirection[1];
                }
            }else{ //如果没击中，继续寻找方向
                board_state[x][y]=1;
                while(!xySearchAble(x+Direction[DirectionIndex][0], y+Direction[DirectionIndex][1])){
                    DirectionIndex+=1;
                    if(DirectionIndex>=4){ 
                        DirectionIndex=0;
                        break;
                    } 
                }
                x=findshipFirstxy[0] + Direction[DirectionIndex][0];
                y=findshipFirstxy[1] + Direction[DirectionIndex][1];
                DirectionIndex+=1;
                if(DirectionIndex>=4)
                    DirectionIndex=0;
        }
    }}
    if(state_findDirection){ //状态处在找到方向的时候
        if(!isHit){ //如果上次没有击中，需要反向搜索，或者停止搜索
            board_state[x][y]=1;
            if(state_findShipFirstEnd){ //如果当前已经是反向搜索了，则停止搜索，终止findship状态
                state_findDirection=0;
                state_searchShip=1;
                state_findShip=0;
                state_findShipFirstEnd=0;
            }else{ // 当前还是正向搜索，则进行反向搜索
                state_findShipFirstEnd=1;
                x=findshipFirstxy[0]-shipDirection[0];
                y=findshipFirstxy[1]-shipDirection[1];
            }
        }else{ //如果上次击中
            if(state_findShipFirstEnd){ //如果是反向搜索
                x=x-shipDirection[0];
                y=y-shipDirection[1];
            }else{
                x=x+shipDirection[0];
                y=y+shipDirection[1];
            }
        }
    }
    return;
    
}
//第一次射击的策略
static void searchShip(int isHit){
    int i,j;
    int max=board_weight[0][0];
    if(isHit){ //击中了，状态转换，state_findShip=0,state_searchShip=1;
        board_state[x][y]=2;
        state_searchShip=0;
        state_findShip=1;
        state_searchDirection=1;
        findshipFirstxy[0]=x; //记录第一次击中的位置
        findshipFirstxy[1]=y;
        findShip(isHit);
        return;
    }else{
        if(notfirst)
            board_state[x][y]=1;
    }
    x=0;
    y=0;
    for(i=0;i<10;i++){   //遍历整个盘，如果此点未被探索，且值得探索，且权值最大，则轰击此点
        for(j=0;j<10;j++){
            if(board_state[i][j]!=0){
                if(max<board_weight[i][j]){
                    max=board_weight[i][j];
                        x=i;
                        y=j;
                }
            }
        }
    }
    board_state[x][y]=1;
}

int StrategyA()
{
    
    int isHit = ALastHitB();
    /*如果是第一次搜索，或者上次没击中，执行FindShip的策略*/
    if(state_searchShip){
        searchShip(isHit);
        notfirst=1;
    }
    //如果上次击中了，也就是isHit，执行SearchBlock()
    else if(state_findShip){
        findShip(isHit);
    }
    
    firetoB(x,y);

    return 0;
}
